<?php

class Login extends CI_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model("model_login");
		$this->load->model('model_usersession');
		if ($this->usersession->userLoggedIn()) {
			redirect("main", "refresh");
		}
	}

	/**
	 * Incarcare pagina login
	 */
	function index() {
		$this->load->view('login/loginform');
	}

	function xlogin() {

		//creare mesaj default
		$aResponse = array("error" => TRUE, "type" => 'validation', "description" => "Utilizator sau parola incorecte!");

		$sUserName = $this->input->post('username');
		$sPsswd = $this->input->post('password');

		//verificare login
		$oUser = $this->model_login->checkUserPassword($sUserName, $sPsswd);

		if ($oUser !== FALSE) {

			//creare backup la baza de date
			//$this->model_login->backup();

			if ($this->usersession->accountConnected($oUser['user_id'])) {
				if ($this->input->post('kickuser') == '1') {
					$this->model_usersession->deleteSession($oUser['user_id']);
					$this->usersession->loginUser($oUser);
					$aResponse = array("error" => FALSE, "type" => 'validation', "description" => "Autentificare reusita.");
				} else {
					$aResponse = array("error" => TRUE, "type" => 'multiplesession', "description" => "Acest utilizator este deja conectat!<br />Doriti sa continuati oricum?<br /><i>(utilizatorul curent va fi delogat)</i>");
				}
			} else {
				$this->usersession->loginUser($oUser);
				$aResponse = array("error" => FALSE, "type" => 'validation', "description" => "Autentificare reusita.");
			}
		}
		echo json_encode($aResponse);
	}
}

?>